//
//  FormTextInputCell.swift
//  UIcomnentTest2
//
//  Created by linhan on 2017/1/14.
//  Copyright © 2017年 test. All rights reserved.
//

import Foundation
import UIKit

protocol SWFormCell:NSObjectProtocol
{
    var value:Any?{get set}
}

class SWTextInputFormCell: UITableViewCell,SWFormCell,UITextFieldDelegate
{
    var textDidChangeCallback:(()->Void)?
    
    private var _cellWidth:CGFloat = 0
    private let _textField = UITextField(frame: CGRectZero)
    private var _titleLabel:UILabel = UILabel()
    
    
    
    init(item: FormItem, cellWidth:CGFloat)
    {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "UITableViewCell")
        _cellWidth = cellWidth
        
        _titleLabel.text = item.title
        _titleLabel.sizeToFit()
        contentView.addSubview(_titleLabel)
        
        let textFieldX:CGFloat = _titleLabel.right + 10
        _textField.frame = CGRectMake(textFieldX, 0, _cellWidth - textFieldX, 40)
        contentView.addSubview(_textField)
        _textField.keyboardType = item.keyboardType
        _textField.delegate = self
        
        selectionStyle = .none
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var value:Any?{
        get{
            return _textField.text
        }
        set{
            _textField.text = newValue as? String
        }
    }
    
    weak var toolBar:UIView? {
        didSet {
            _textField.inputAccessoryView = toolBar
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
    {
        textDidChangeCallback?()
        return true
    }
    
    
    
}//end class
